package com.service.admin.admin.controller;

import com.service.admin.admin.controller.request.SysUserPageReq;
import com.service.admin.admin.controller.request.SysUserReq;
import com.service.admin.admin.controller.request.UserResetPwdReq;
import com.service.admin.admin.controller.response.SysUserResp;
import com.service.admin.admin.model.SysUserPage;
import com.service.admin.admin.service.SysUserService;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.CommonPageRep;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.admin.support.SessionUtil;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.service.core.constant.BusinessStatus.DELETE_SELF_ERROR;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年08月02日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "管理员")
@RestController
@RequestMapping("/sys-user")
public class SysUserController extends BaseController {

    @Autowired
    private SessionUtil sessionUtil;

    @Autowired
    private SysUserService userService;

    @ApiOperation("管理员分页")
    @PostMapping("/list")
    public PageResponse<SysUserPage> pageUser(@ApiParam @RequestBody SysUserPageReq userReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", userReq, uuid);
        CommonPageRep<SysUserPage> result = userService.pageSysUser(userReq);
        log.info("请求结束： UUID:{}", uuid);
        return successQueryPageResponse(result);
    }

    @ApiOperation("保存或者更新用户")
    @PostMapping("/save")
    public BaseResponse saveUser(@ApiParam @RequestBody SysUserReq sysUserReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        userService.saveSysUser(sysUserReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @ApiOperation("用户详情")
    @GetMapping("/detail/{id}")
    public OneResponse userDetail(@ApiParam @PathVariable("id") Integer id) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        SysUserResp userResp = userService.getSysUserDetailById(id);
        log.info("请求结束： UUID:{}", uuid);
        return successQueryOneResponse(userResp);
    }

    @ApiOperation("批量删除用户")
    @PostMapping("/delete")
    public OneResponse delete(@ApiParam @RequestBody List<Integer> ids) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", ids, uuid);

        if (ObjectUtils.isEmpty(ids)){
            log.info("无效请求,ids为空 uuid:{}", uuid);
            throw new CommonException(HttpStatus.BAD_REQUEST.value(), "ids为空");
        }
        Integer userId = sessionUtil.getUser().getId();
        if(ids.contains(userId)){
            log.info("不允许删除自身账号 userId:{}, uuid:{}", userId, uuid);
            throw new CommonException(DELETE_SELF_ERROR.value(), "不允许删除自身账号");
        }

        userService.deleteSysUser(ids);
        log.info("请求结束： UUID:{}", uuid);
        return successQueryOneResponse(null);
    }

    @ApiOperation("重置密码")
    @PostMapping("/reset-pwd")
    public BaseResponse resetPwd(@ApiParam @RequestBody @Validated UserResetPwdReq resetPwdReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        userService.resetPwd(resetPwdReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }




}
